// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios, {Axios} from "axios";
import './plugins/element.js'
import store from "./store";
import base from "./api/base"
import md5 from 'js-md5'
import all from "./api/base";
import qs from "qs"
import VueAMap from "vue-amap";
import global from "./js/global"

Vue.config.productionTip = false
Vue.prototype.$axios = axios
Vue.prototype.$md5 = md5
Vue.prototype.$qs = qs
Vue.prototype.global = global
axios.defaults.baseURL = '/api'
axios.defaults.headers.post['Content-Type'] = 'application/josn'


//全局路由守卫
axios.interceptors.request.use(config =>{
  let noTokenUrls = ["/","/getValidateCode","/getEmailValidate","/registerUser","/myLoginForm","/getAreaGroupFirst"]
  if (noTokenUrls.indexOf(config.url)>-1){
  }else {
    if (localStorage.getItem("Authorization")){
      config.headers.Authorization = localStorage.getItem("Authorization")
    }
  }
  return config;
},error => {
  return Promise.reject(error)
})

//响应回来token是否过期
axios.interceptors.response.use(response => {
    if(response.data.code === 403){
      Vue.prototype.$message({
        type: "error",
        message: "未授权！"
      });
      return ;
    }
    //和后端token失效返回码约定406
    if (response.data.code == 406) {
      // 引用elementui message提示框
      Vue.prototype.$message({
        type: "error",
        message: "身份已过期，请重新登录"
      });
      //清除token
      localStorage.removeItem('Authorization');
      //跳转
      router.push({name: 'Login'});
    } else {
      return response
    }
  },
  error => {
    return Promise.reject(error);
  })



/* eslint-disable no-new */
Vue.use(all);


setTimeout(() => {//避免调用出错
  Vue.use(VueAMap);
  VueAMap.initAMapApiLoader({
    key: 'a4c1e290cce7c8c2dd9f8d3acee30f1a',
    plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor'],
    // 默认高德 sdk 版本为 1.4.4
    v: '1.4.4'
  });
}, 0);
new Vue({
  el: '#app',
  router,
  store,
  components: {App},
  template: '<App/>'
})

